//
//  CityPickerView.m
//  CarTown
//
//  Created by xionghao on 15/8/13.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "CityPickerView.h"


#import "Address.h"
#define BgColor [[UIColor alloc]initWithRed:44/255.0 green:44/255.0 blue:44/255.0 alpha:0.5]

@interface CityPickerView()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UIView *pickerbg;

@property (nonatomic,strong) NSMutableArray *mprarray;
@property (nonatomic,strong) NSMutableArray *mcrarray;
@property (nonatomic,strong) NSMutableArray *mdrarray;
@property (nonatomic,assign) NSInteger coloums;
@end

@implementation CityPickerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (IBAction)cancelclick:(id)sender {
   [self hide];
    
}
- (IBAction)okclick:(id)sender {
    
    if ([_citydelegate respondsToSelector:@selector(citypicker:)]) {
        NSString *location;
        Address *pitem=self.mprarray[[self.picker selectedRowInComponent:0]];
        Address *citem=self.mcrarray[[self.picker selectedRowInComponent:1]];
        Address *ditem;
        if (self.coloums==3) {
             ditem=self.mdrarray[[self.picker selectedRowInComponent:2]];
        }
       
        if ([pitem.area_name isEqualToString:citem.area_name]) {
            if (self.coloums==3) {
                if ([citem.area_name isEqualToString:ditem.area_name]) {
                    location=ditem.area_name;
                }else{
                    location=[NSString stringWithFormat:@"%@%@",citem.area_name,ditem.area_name];
                }
            }else{
                location=citem.area_name;
            }
            
        }else{
            if (self.coloums==3) {
               location=[NSString stringWithFormat:@"%@%@%@",pitem.area_name,citem.area_name,ditem.area_name];
            }else{
                location=[NSString stringWithFormat:@"%@%@",pitem.area_name,citem.area_name];
            }
            
        }
        [_citydelegate citypicker:location];
    }
    if (self.coloums==3) {
        if ([_citydelegate respondsToSelector:@selector(citypicker:city:dist:)]) {
            Address *pitem=self.mprarray[[self.picker selectedRowInComponent:0]];
            Address *citem=self.mcrarray[[self.picker selectedRowInComponent:1]];
            Address *ditem=self.mdrarray[[self.picker selectedRowInComponent:2]];
            [_citydelegate citypicker:pitem.area_id city:citem.area_id  dist:ditem.area_id ];
        }
        if ([_citydelegate respondsToSelector:@selector(citypicker:withPicker:)]) {
            NSString *location;
            Address *pitem=self.mprarray[[self.picker selectedRowInComponent:0]];
            Address *citem=self.mcrarray[[self.picker selectedRowInComponent:1]];
            Address *ditem=self.mdrarray[[self.picker selectedRowInComponent:2]];
            if ([pitem.area_name isEqualToString:citem.area_name]) {
                if ([citem.area_name isEqualToString:ditem.area_name]) {
                    location=ditem.area_name;
                }else{
                    location=[NSString stringWithFormat:@"%@%@",citem.area_name,ditem.area_name];
                }
            }else{
                location=[NSString stringWithFormat:@"%@%@%@",pitem.area_name,citem.area_name,ditem.area_name];
            }
            [_citydelegate citypicker:location withPicker:self];
        }

    }
       [self hide];
}

-(NSMutableArray *)mprarray{
    if (_mprarray==nil) {
        _mprarray=[NSMutableArray array];
        [_mprarray addObjectsFromArray:[Address getPro]];
    }
    return _mprarray;
}
-(NSMutableArray *)mcrarray{
    if (_mcrarray==nil) {
        _mcrarray=[NSMutableArray array];
        Address *item=self.mprarray[0];
        [_mcrarray addObjectsFromArray:[Address getCity:item.area_id]];
    }
    return _mcrarray;
}
-(NSMutableArray *)mdrarray{
    if (_mdrarray==nil) {
        _mdrarray=[NSMutableArray array];
         Address *item=self.mcrarray[0];
        [_mdrarray addObjectsFromArray:[Address getDisct:item.area_id]];
    }
    return _mdrarray;
}
-(instancetype)initWithDelegate:(id<CityPickerDelegate>)delegate WithColoum:(NSInteger)coloums{
    self=[[[NSBundle mainBundle]loadNibNamed:@"CityPickerView" owner:nil options:nil]lastObject];
    self.picker.delegate=self;
    self.picker.dataSource=self;
    self.backgroundColor=[UIColor clearColor];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
    [self addGestureRecognizer:tap];
    if (delegate) {
        self.citydelegate=delegate;
    }
    if (coloums==0) {
        self.coloums=3;
        
    }else{
        self.coloums=coloums;
    }
    return self;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 0.0f, [pickerView rowSizeForComponent:component].width-12, [pickerView rowSizeForComponent:component].height)];
    }
    retval.textAlignment = NSTextAlignmentCenter;
    retval.font=[UIFont systemFontOfSize:(XHScreenwidth==320)?13:14];
    switch (component) {
        case 0:
        {
            Address *item=self.mprarray[row];
            retval.text=item.area_name;
        }
            break;
        case 1:
        {
            Address *item=self.mcrarray[row];
            retval.text=item.area_name;
        }
            break;
        case 2:
        {
            Address *item=self.mdrarray[row];
            retval.text=item.area_name;
        }
            break;
        default:
            break;
    }
  
    return retval;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return self.coloums;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.mprarray.count;
    } else if (component == 1) {
        return self.mcrarray.count;
    } else {
        return self.mdrarray.count;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    switch (component) {
        case 0:
        {
            Address *item=self.mprarray[row];
            return item.area_name;
        }
            break;
        case 1:
        {
            Address *item=self.mcrarray[row];
            return item.area_name;
        }
            break;
        case 2:
        {
            Address *item=self.mdrarray[row];
            return item.area_name;
        }
            break;
        default:
            return nil;
            break;
    }
    
  
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component==0) {
        [self.mcrarray removeAllObjects];
       
        Address *item=self.mprarray[row];
        NSArray *array=[Address getCity:item.area_id];
        [self.mcrarray addObjectsFromArray:array];
        Address *cadress=array[0];
        
        if (self.coloums==3) {
             [self.mdrarray removeAllObjects];
             [self.mdrarray addObjectsFromArray:[Address getDisct:cadress.area_id]];
        }
       
        
    }
    [pickerView selectedRowInComponent:1];
    
    [pickerView reloadComponent:1];
    
    if (self.coloums==3) {
        [pickerView selectedRowInComponent:2];
        if (component==1) {
            [self.mdrarray removeAllObjects];
            Address *item=self.mcrarray[row];
            [self.mdrarray addObjectsFromArray:[Address getDisct:item.area_id]];
            [pickerView selectRow:0 inComponent:2 animated:YES];
        }
        [pickerView reloadComponent:2];
    }
    
   
}

-(void)showinView:(UIView *)view{
   
    self.frame=CGRectMake(0,0, XHScreenwidth, XHScreenheight);
     self.pickerbg.y=XHScreenheight;
    [view.window addSubview:self];
    [UIView animateWithDuration:0.5 animations:^{
        self.pickerbg.y=XHScreenheight-200;
        self.backgroundColor=BgColor;
    }];
}
-(void)hide{
    [UIView animateWithDuration:0.5 animations:^{
        self.pickerbg.y=XHScreenheight;
          self.backgroundColor=[UIColor clearColor];
    } completion:^(BOOL finished) {
       
        [self removeFromSuperview];
       
    }];
}
@end
